/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#include "DiaglogUSBSetting.h"
#include "DataConvertor.h"
using namespace BurnDlg;
namespace {
    const int DEFAULT_TOTALNUM = 4;
    const int DEFAULT_NUMPERLINE = 8;
}

DiaglogUSBSetting::DiaglogUSBSetting(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::DiaglogUSBSetting),
      totalNum(DEFAULT_TOTALNUM),
      numPerLine(DEFAULT_NUMPERLINE)
{
    ui->setupUi(this);

    this->InitUi();
}

DiaglogUSBSetting::~DiaglogUSBSetting()
{
    if (ui != nullptr) {
        delete ui;
        ui = nullptr;
    }

    if (pReg0 != nullptr) {
        delete pReg0;
        pReg0 = nullptr;
    }

    if (pReg1 != nullptr) {
        delete pReg1;
        pReg1 = nullptr;
    }
}

void DiaglogUSBSetting::InitUi()
{
    if (ui == nullptr) {
        return;
    }

    setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
    setWindowTitle(QObject::tr("USB Setting"));

    ui->lineEditTotleNum->setText(QString::number(totalNum));
    ui->lineEditNumPerLine->setText(QString::number(numPerLine));

    QRegExp rx0("^[1-9]{1}$|^1[0-9]$|^2[0]$");
    pReg0 = new QRegExpValidator(rx0, this);
    if (pReg0 != nullptr) {
        ui->lineEditTotleNum->setValidator(pReg0);
    }

    QRegExp rx1("^[4-8]{1}");
    pReg1 = new QRegExpValidator(rx1, this);
    if (pReg1 != nullptr) {
        ui->lineEditNumPerLine->setValidator(pReg1);
    }

    connect(ui->buttonOK, &QPushButton::clicked, this, &DiaglogUSBSetting::SlotOkButtonClick);
    connect(ui->buttonCancel, &QPushButton::clicked, this, &DiaglogUSBSetting::SlotCancelButtonClick);
}

void DiaglogUSBSetting::SlotOkButtonClick()
{
    if (ui == nullptr) {
        return;
    }

    totalNum = DataConvertor::Str2U32DecHex(ui->lineEditTotleNum->text());
    numPerLine = DataConvertor::Str2U32DecHex(ui->lineEditNumPerLine->text());

    emit SignalSettingSuccess();
    this->close();
}

void DiaglogUSBSetting::SlotCancelButtonClick()
{
    if (ui == nullptr) {
        return;
    }

    ui->lineEditTotleNum->setText(QString::number(totalNum));
    ui->lineEditNumPerLine->setText(QString::number(numPerLine));

    this->close();
}

void DiaglogUSBSetting::SetMultipleBurnParam(unsigned int totalNumber, unsigned int numPerLines)
{
    totalNum = totalNumber;
    numPerLine = numPerLines;
}
void DiaglogUSBSetting::GetMultipleBurnParam(unsigned int &totalNumber, unsigned int &numPerLines) const
{
    totalNumber = totalNum;
    numPerLines = numPerLine;
}
void DiaglogUSBSetting::LoadDlgConfig() const
{
    ui->lineEditTotleNum->setText(QString::number(totalNum));
    ui->lineEditNumPerLine->setText(QString::number(numPerLine));
}
